package com.tangmu.rabbits.ui.fragment.optimization;

import android.content.res.Resources;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.adapter.HomeActivitiesAdapter;
import com.tangmu.rabbits.base.BaseOneFragment;
import com.tangmu.rabbits.ui.activity.home.BrandIflashbuyActivity;
import com.tangmu.rabbits.ui.activity.home.NineElementActivity;
import com.tangmu.rabbits.ui.activity.home.TodayChooseActivity;
import com.tangmu.rabbits.ui.activity.optimization.ScreenActivity;
import com.tangmu.rabbits.ui.fragment.BlankFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 优选
 */
public class OptimizationFragment extends BaseOneFragment implements ViewPager.OnPageChangeListener, View.OnClickListener, HomeActivitiesAdapter.OnItemClickLisener {

    public static final String TAG = "OptimizationFragment";

    @BindView(R.id.tv_header_title)
    TextView tvHeaderTitle;
    @BindView(R.id.iv_header_right)
    TextView ivHeaderRight;
    @BindView(R.id.op_headerView)
    LinearLayout opHeaderView;
    @BindView(R.id.optimization_top_recyclerView)
    RecyclerView topRecyclerView;

    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    /**
     * 筛选
     */
    @BindView(R.id.optimization_tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.vp_optimization)
    ViewPager mViewPager;
    @BindView(R.id.tv_goods_all)
    TextView tvAll;
    @BindView(R.id.tv_goods_sales_volume)
    TextView tvSales;
    @BindView(R.id.tv_goods_newest)
    TextView tvNewest;
    @BindView(R.id.tv_goods_price)
    TextView tvPrice;

    private List<Fragment> fragments = new ArrayList<>();
    private HomeActivitiesAdapter adapter;
    private List<Integer> datas = new ArrayList<>();
    private String[] tabs = {"全部", "女装", "男装", "内衣", "美妆", "居家", "母婴", "食品", "鞋包"};

    public OptimizationFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColor(R.color.transparent).titleBar(R.id.op_headerView).init();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_optimization;
    }

    @Override
    protected void initView() {
        tvHeaderTitle.setText(mActivity.getResources().getString(R.string.optimization));
        ivHeaderRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ScreenActivity.class);
            }
        });

        for (int i = 0; i < 4; i++) {
            datas.add(i);
        }

        topRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
        //topRecyclerView.setNestedScrollingEnabled(false);
        //topRecyclerView.setFocusableInTouchMode(false);
        //topRecyclerView.requestFocus();
        adapter = new HomeActivitiesAdapter(mActivity, datas);
        topRecyclerView.setAdapter(adapter);
        adapter.setItemClickLisener(this);

        for (int i = 0; i < tabs.length; i++) {
            fragments.add(BlankFragment.newInstance(tabs[i]));
        }
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.addOnPageChangeListener(this);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
       /* mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, -5, -5);
            }
        });*/
        initTabsClick();
    }

    private void initTabsClick() {
        tvAll.setOnClickListener(this);
        tvSales.setOnClickListener(this);
        tvNewest.setOnClickListener(this);
        tvPrice.setOnClickListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 调整indicator的间距 样式
     *
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    @Override
    public void onClick(View v) {
        if (v == tvAll) {
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        } else if (v == tvSales) {
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        } else if (v == tvNewest) {
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        } else if (v == tvPrice) {
            tvPrice.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_select_color));
            tvAll.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvSales.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
            tvNewest.setTextColor(ContextCompat.getColor(mActivity, R.color.tab_t_color));
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        if (position == 0) {
            startActivity(TodayChooseActivity.class);
        } else if (position == 1) {
            startActivity(NineElementActivity.class);
        } else if (position == 2) {
            startActivity(BrandIflashbuyActivity.class);
        } else if (position == 3) {
            startActivity(NineElementActivity.class);
        }
    }
}
